package LeetCode;
//将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的
public class Num21 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
//        判断链表是否为null
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }
        ListNode dummyHead = new ListNode(101);
        ListNode last = dummyHead;
//        尾插法连接链表
        while (list1 != null && list2 != null) {
            if (list1.val <= list2.val) {
                last.next = list1;
                last = list1;
                list1 = list1.next;
            }else {
                last.next = list2;
                last = list2;
                list2 = list2.next;
            }
        }
//        当任一链表走到null时
        if (list1 == null) {
            last.next = list2;
        }else {
            last.next = list1;
        }
        return dummyHead.next;
    }
}
